# encoding:utf-8
from Write_data import*
import spider_weather
import pandas as pd
import spider_valley
import re
# import numpy as np


class spider_modular_main():

    def __init__(self, outputFilename="dataset/dataset.csv", place=0):

        # 数据成员
        self.outputData_jiu = pd.DataFrame()  # 最终数据dataframe类型
        self.outputData_si = pd.DataFrame()  # 最终数据dataframe类型
        self.outputData = pd.DataFrame()  # final
        self.outputFilename = outputFilename  # 保存到文件名
        self.place = place
        self.valleyData_jiu = pd.DataFrame()
        self.valleyData_si = pd.DataFrame()
        self.weatherData_jiu = pd.DataFrame()
        self.weatherData_si = pd.DataFrame()

        # 函数
        self.main()

    def main(self):

        # 数据初始化
        self.initData()

        # 利用数据库左外连接进行数据合并
        self.mergeData()

    # 封装
    # 数据初始化

    def initData(self):
        if self.place == 0:
            # 700-2180
            self.valleyData_jiu = spider_valley.valley_spider(
                place=0, dataAmount=2180, fileName="dataset/九寨沟客流.csv").valleyData  # 700

            # 读取天气数据
            self.weatherData_jiu = spider_weather.Weather_spider(
                place=0, fileName="dataset/九寨沟天气.csv").weatherData

        elif self.place == 1:
            # 300-1820
            self.valleyData_si = spider_valley.valley_spider(
                place=1, dataAmount=1820, fileName="dataset/四姑娘山客流.csv").valleyData  # 300

            # 读取天气数据
            self.weatherData_si = spider_weather.Weather_spider(
                place=1, fileName="dataset/四姑娘山天气.csv").weatherData

    # 合并数据
    def mergeData(self):

        if self.place == 0:
            self.outputData_jiu = pd.merge(
                self.valleyData_jiu, self.weatherData_jiu, how="left", on="date")
            self.outputData_jiu = self.outputData_jiu.drop('风力', 1)
            self.delSymbol("最高温度", self.outputData_jiu)
            self.delSymbol("最低温度", self.outputData_jiu)
            # 筛选指定年份的数据
            df = self.outputData_jiu
            df1 = df[df['date'].str.contains('2013')]
            df2 = df[df['date'].str.contains('2014')]
            df3 = df[df['date'].str.contains('2015')]
            df4 = df[df['date'].str.contains('2016')]
            self.outputData_jiu = pd.concat(
                [df1, df2, df3, df4], axis=0, ignore_index=True)

        elif self.place == 1:
            self.outputData_si = pd.merge(
                self.valleyData_si, self.weatherData_si, how="left", on="date")
            self.outputData_si = self.outputData_si.drop('风力', 1)

            # 异常处理：不存在所要删除的列名
            try:
                self.delSymbol("最高温度", self.outputData_si)
                self.delSymbol("最低温度", self.outputData_si)
            except:
                print("不存在所要删除的列名")

            # 筛选指定年份的数据
            df = self.outputData_si
            df1 = df[df['date'].str.contains('2016')]
            df2 = df[df['date'].str.contains('2017')]
            df3 = df[df['date'].str.contains('2018')]
            df4 = df[df['date'].str.contains('2019')]
            self.outputData_si = pd.concat(
                [df1, df2, df3, df4], axis=0, ignore_index=True)

         # 最终赋值
        if self.place == 0:
            df = self.outputData_jiu
        else:
            df = self.outputData_si

        Write_data(self.outputFilename, df)
        self.outputData = df
        # print(self.outputData.dtypes)
        # print(self.outputData)

    # 删除摄氏度
    def delSymbol(self, colName="最高温度", data=None):
        mydata = data
        mydata[colName] = mydata[colName].apply(
            lambda x: self.processSymbol(x))

    # 处理摄氏度
    def processSymbol(self, data_tem):
        try:
            return re.findall("\d+", data_tem)[0]
        except:
            # print("小心数据有空值")
            pass


# spider_modular_main(place=1)